from unittest.mock import MagicMock

from django.conf import settings
from django.core import files
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils import timezone

from .forms import ServerApplicationForm, LoginForm
from .models import ServerApplication
from .views import ServerApplyView
from .libs import utils, demail


def create_demo_application():
    _file = SimpleUploadedFile('best_file_eva.txt', 'these are the file contents!'.encode())

    d = {
        'user_id': 1234,
        'user_name': 'user_name',
        'project_name': 'project_name',
        'server_count': 1,
        'budget': 1000,
        'detail_file': _file,
        'remark': 'remark',
    }

    application = ServerApplication(**d)
    application.save()

    d = {
        'approver_name': 'approver_name',
        'approver_email': 'approver_email@xx.com',
        'approver_respectful_name': 'approver_respectful_name'
    }

    application.approval_set.create(**d)
    return application


class UtilsMethodTests(TestCase):

    def test_get_boss_approver_info(self):
        APPROVERS = (
            ('a@zhenai.com', '范成涛,范总'),
            ('b@zhenai.com', '徐晗路,徐总'),
        )

        info = utils.get_boss_approver_info(APPROVERS)
        self.assertEqual(info.get('a@zhenai.com').get('name'), '范成涛')
        self.assertEqual(info.get('a@zhenai.com').get('respectful_name'), '范总')
        self.assertEqual(info.get('b@zhenai.com').get('name'), '徐晗路')
        self.assertEqual(info.get('b@zhenai.com').get('respectful_name'), '徐总')

    def test_get_approval_text(self):
        application = create_demo_application()
        text = utils.get_approval_text(application)
        self.assertEqual(text, '等待 approver_name 审批')

        for appr in application.approval_set.all():
            appr.result = True
            appr.save()

        text = utils.get_approval_text(application)
        self.assertEqual(text, '审批完成')

    def test_get_next_approval(self):
        application = create_demo_application()
        appr = utils.get_next_approval(application)
        self.assertEqual(appr.result, None)

        # change first approval result
        _appr = application.approval_set.all()[0]
        _appr.result = True
        _appr.save()

        appr = utils.get_next_approval(application)
        self.assertEqual(appr, None)

    def test_get_last_approval(self):
        application = create_demo_application()
        appr = utils.get_last_approval(application)
        self.assertEqual(appr, None)

        # change first approval result
        _appr = application.approval_set.all()[0]
        _appr.result = True
        _appr.save()

        appr = utils.get_last_approval(application)
        self.assertEqual(appr.result, True)

    def test_gen_approval_email_content(self):
        application = create_demo_application()
        approval = application.approval_set.all()[0]
        c = utils.gen_approval_email_content(application, approval)

        path = reverse('application:approval', kwargs={'app_id': application.id})
        url = '%s%s' % (settings.SERVER_URL, path)
        d = {
            'receiver': 'approver_respectful_name',
            'applicant': 'user_name',
            'url': url,
        }
        _c = settings.NEXT_APPROVAL_EMAIL_TEMPLATE.format(**d)
        self.assertEqual(c, _c)

    def test_gen_reject_email_content(self):
        application = create_demo_application()
        approval = application.approval_set.all()[0]
        c = utils.gen_reject_email_content(application, approval)

        path = reverse('application:detail', kwargs={'app_id': application.id})
        url = '%s%s' % (settings.SERVER_URL, path)
        d = {
            'receiver': 'user_name',
            'approver': 'approver_respectful_name',
            'url': url,
        }
        _c = settings.REJECT_EMAIL_TEMPLATE.format(**d)
        self.assertEqual(c, _c)

    def test_gen_agree_email_content(self):
        application = create_demo_application()
        approval = application.approval_set.all()[0]
        c = utils.gen_agree_email_content(application, approval)

        path = reverse('application:detail', kwargs={'app_id': application.id})
        url = '%s%s' % (settings.SERVER_URL, path)
        d = {
            'receiver': 'user_name',
            'approver': 'approver_respectful_name',
            'url': url,
        }
        _c = settings.AGREE_EMAIL_TEMPLATE.format(**d)
        self.assertEqual(c, _c)

    def test_gen_pass_email_content(self):
        application = create_demo_application()
        c = utils.gen_pass_email_content(application)

        path = reverse('application:detail', kwargs={'app_id': application.id})
        url = '%s%s' % (settings.SERVER_URL, path)
        d = {
            'receiver': 'user_name',
            'url': url,
        }
        _c = settings.PASS_EMAIL_TEMPLATE.format(**d)
        self.assertEqual(c, _c)

    def test_email(self):
        e = demail.Email()
        self.assertTrue(e.ok)
